<?php

namespace app\backend\controller;

use app\backend\model\Category as CategoryDao;
use app\backend\model\Article as articleDao;
use think\Db;
use think\Request;

class Article extends Common
{
    protected $categoryDao;

    protected $articleDao;

    public function __construct(Request $request, articleDao $article, CategoryDao $category)
    {
        parent::__construct($request);
        $this->articleDao = $article;
        $this->categoryDao = $category;
        $category = $this->categoryDao->field('category_id, category_name')->select();
        $this->assign('category_name', $category);
    }

    public function index(Request $request)
    {
        $vars = $request->param();
        if (!empty($vars['search_name'])) {
            $this->map['article_name'] = ['like', "%{$vars['search_name']}%"];
        }

        $lists = $this->articleDao->with('category')->where($this->map)
            ->paginate($this->pageSize, false, ['query' => $this->map]);

        $this->assign('lists', $lists);
        $this->assign('map', $vars);
        $this->assign('page', $lists->render());
        return $this->fetch();
    }

    public function create(Request $request)
    {
        $vars = $request->param();

        $res = $this->validate($vars, 'Article.create');
        if ($res !== true)
        {
            return ['status' => 0, 'msg' => $res];
        }
        $data = $this->articleDao->allowField(true)->create($vars);
        if ($data !== false) {
            return ['status' => 1, 'msg' => 'succ'];
        }
        return ['status' => 0, 'msg' => '保存失败'];
    }

    public function read(Request $request)
    {
        $vars = $request->param();
        $category = $this->categoryDao->select();
        if (isset($vars['article_id'])) {
//            $data = Db::table('blog_article')
//                ->alias('a')
//                ->join('blog_category b', 'a.category_id = b.category_id')
//                ->where('article_id', $vars['article_id'])->find();
           $test = $this->articleDao->with('category')->select();
           // $test = $this->categoryDao->select();
            halt($test);

            $data = $this->articleDao->with('category')->where('article_id', $vars['article_id'])->find();

            $tree = make_option_tree_for_select($category, 0, $data['category_id']);
            if ($request->isAjax()) {

                if ($data !== false) {
                    $data['tree'] = $tree;
                    return ['status' => 1, 'msg' => 'succ', 'data' => $data];
                }
                return ['status' => 0, 'msg' => '查找失败'];
            }

            $this->assign('tree', $tree);
            $this->assign('data', $data);

            return $this->fetch();
        }


        $tree = make_option_tree_for_select($category, 0);

        $this->assign('tree', $tree);

        return $this->fetch();
    }

    public function delete(Request $request)
    {
        $vars = $request->param();

        $data = $this->articleDao->destroy($vars['article_id']);

        if ($data !== false) {
            return ['status' => 1, 'msg' => 'succ'];
        }
        return ['status' => 0, 'msg' => '删除失败'];
    }

    public function update(Request $request)
    {
        $vars = $request->param();
        $res = $this->validate($vars, 'Article.update');
        if ($res !== true)
        {
            return ['status' => 0, 'msg' => $res];
        }
        $data = $this->articleDao->where("article_id", $vars['article_id'])->update($vars);

        if ($data !== false) {
            return ['status' => 1, 'msg' => 'succ'];
        }
        return ['status' => 0, 'msg' => '更新失败'];
    }
}